<!DOCTYPE html>
<html>
<head>
    <title>文章编辑</title>
    {% include 'system/common/header.html' %}</head>
    <link href="{{ url_for('static', filename='blog/editor.md-master/css/editormd.css') }}" rel="stylesheet"/>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
              <div class="layui-form-item layui-hide">
                  <label class="layui-form-label">文章编号</label>
                  <div class="layui-input-block">
                      <input type="text" value="{{ article.id }}" name="articleId" lay-verify="title"
                             class="layui-input">
                  </div>
              </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label">文章标题</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ article.title }}" name="articleTitle" lay-verify="title"
                               autocomplete="off" placeholder="请输入文章名称" class="layui-input">
                    </div>
                  </div>

                  <div class="layui-inline">

                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                      <input type="radio" name="isDel" value="0" title="正常">
                      <input type="radio" name="isDel" value="1" title="删除">
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label">所属分类</label>
                    <div class="layui-input-inline">
                      <select name="articleCategoryId">
                        {% for category in categorys %}
                          {% if category.id == article.category_id %}
                          <option value="{{ category.id}}" selected>{{ category.name}}</option>
                          {% else %}
                          <option value="{{ category.id}}">{{ category.name}}</option>
                          {% endif %}
                        {% endfor %}
                      </select>
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">标签</label>
                  <div class="layui-input-block">
                    {% for tag in all_tags %}
                      {% if tag.id in article_tag_ids %}
                        <input type="checkbox" name="articleTagIds" lay-skin="tag" title="{{ tag.name }}" value="{{ tag.id }}" checked>
                      {% else %}
                        <input type="checkbox" name="articleTagIds" lay-skin="tag" title="{{ tag.name }}" value="{{ tag.id }}">
                      {% endif %}
                    {% endfor %}
                  </div>
                </div> 
                <div class="layui-form-item">
                    <label class="layui-form-label">文章内容</label>
                    <div class="layui-input-block" id="blog-editormd">
                        <textarea name="articleContent" style="display:none;">{{ article.content }}</textarea>                               
                    </div>
                </div>
              </div>                
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="article-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.0/zepto.js" integrity="sha512-+ab24sSOgbciah+hyqPk/XqGOooAInsofud5JB4AdrRfahahDA/WWnnm6dS/bl7UmN8RW8ZGp85EoWU/aFTUhA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="/static/index/layui/lay/modules/jquery.js"></script>
<script src="/static/blog/editor.md-master/editormd.js"></script>
<script>
  layui.use(['form', 'jquery'], function () {
    let form = layui.form
    let $ = layui.jquery

    let blogEditor = editormd("blog-editormd", {
      width: "100%",
      height: 640,
      syncScrolling: "single",
      path: "/static/blog/editor.md-master/lib/"
  });    

    $("[name=isDel]").each(function () {
      if({{ article.is_del }}==0){
          type="0";
      }else{
          type="1"
      }
      // 根据值回显
      if ($(this).val() == type) {
          $(this).attr("checked", "checked");
          return false;
      }
    })
    form.render(); //更新全部

    form.on('submit(article-update)', function (data) {
      // 获取选中的 checkbox 的值
      var checkboxValues = [];
      $(".layui-form-checkbox.layui-form-checked").each(function() {
        var inputValue = $(this).prev("input").val();
        checkboxValues.push(inputValue)
      });
      data.field.articleTagIds = checkboxValues
      $.ajax({
        url: '/blog/article/update',
        data: JSON.stringify(data.field),
        dataType: 'json',
        contentType: 'application/json',
        type: 'put',
        success: function (result) {
          if (result.success) {
            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
              parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
              parent.layui.table.reload('role-table')
            })
          } else {
            layer.msg(result.msg, { icon: 2, time: 1000 })
          }
        }
      })
      return false
    })
  })
</script>
<script>
</script>
</body>
</html>